var async = require('async');
var consul_helper = require('./consul_helper.js');
var consul = require('consul')({
  promisify: consul_helper.fromCallback
});
var getService = consul_helper.getService.bind(consul);

var Q = require('q');
var request = require('request');

function Client() {
  var client = this;
  this.scheduler_address = '';
  this.buildTask = function(setting, callback) {
    var url = '';
    async.series([function(next) {
      if (!client.scheduler_address) {
        return client.init(next);
      }
      return next();
    }, function(next) {
      url = client.scheduler_address + '/buildTask';
      console.log(url);
      request({
        uri: url,
        method: 'POST',
        json: setting
      }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
          return callback('', {
            "msg": body.msg,
            "setting": body.setting
          });
        }
        else
          return next(error);
      });
    }], function(error) {
      callback(error);
    });

  }

  this.deleteTask = function(taskId, callback) {
    var url = '';
    var listName = '';
    async.series([function(next) {
      if (!client.scheduler_address) {
        return client.init(next);
      } else {
        return next();
      }
    }, function(next) {
      url = client.scheduler_address + '/delete/' + taskId;
      request({
        uri: url
      }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
          return callback('', body);
        }
        return callback(error);
      });
    }], function(error) {
      callback(error);
    });

  }

  this.stopTask = function(id, callback) {
    var url = '';
    var listName = '';
    async.series([function(next) {
      if (!client.scheduler_address) {
        return client.init(next);
      }
      else
        return next();
    }, function(next) {

      url = client.scheduler_address + '/stop';
      console.log('这个url', url);

      request({
        uri: url,
        method: 'POST',
        json: {
          'id': id
        }
      }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
          return callback('', body);
        }
        return next(error);
      });
    }], function(error) {
      callback(error);
    });
  }

  this.updateTask = function(taskId, setting, callback) {
    var url = '';
    var listName = '';
    async.series([function(next) {
      if (!client.scheduler_address) {
        return client.init(next);
      }
      else
        return next();
    }, function(next) {
      url = client.scheduler_address + '/update';
      request({
        uri: url,
        method: 'POST',
        json: {
          'id': taskId,
          'setting': setting
        }
      }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
          return callback('', body);
        }
        return next(error);
      });
    }], function(error) {
      callback(error);
    });
  }

  this.getList = function(callback) {
    var url = '';
    var list = {};
    async.series([function(next) {
      if (!client.scheduler_address) {
        return client.init(next);
      }
      return next();
    }, function(next) {
      url = client.scheduler_address + '/getTasks';
      console.log(url);
      request({
        uri: url
      }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
          list = body;
          return next();
        }
        else
          return next(error);
      });
    }], function(error) {
      if (error) {
        return callback(error);
      }
      return callback('', list)
    });
  }


  // 暂时写死path，需修改
  this.start = function(id, callback) {
    var list = null;
    async.series([function(next) {
      if (!client.scheduler_address) {
        return client.init(next);
      }
      return next();
    }, function(next) {
      url = client.scheduler_address + '/start/' + id;
      console.log(url);
      request({
        uri: url

      }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
          list = body;
          console.log('测试', body);
          return next();
        }
        else
          return next(error);
      });
    }], function(error) {
      if (error) {
        return callback(error);
      }
      return callback('', {
        'success': true,
        'setting': list
      })
    });
  }

  // 获取状态
  this.getStatus = function(callback) {
    var list = null;
    async.series([function(next) {
      if (!client.scheduler_address) {
        return client.init(next);
      }
      return next();
    }, function(next) {
      url = client.scheduler_address + '/getStatus';
      request({
        uri: url
      }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
          list = body;
          console.log('测试', body);
          return next();
        }
        else
          return next(error);
      });
    }], function(error) {
      if (error) {
        return callback(error);
      }
      return callback('', list);
    });
  }
  this.init = function(next) {
    return getService('Scheduler')
      .then(function(address) {
        client.scheduler_address = address;
        return next();
      })
      .catch(function(e) {
        throw e;
      });
  }
}

var client = null;

var Singleton = function() {
  if (client === null) {
    client = new Client();
  }
  return client;

};



module.exports = {
  getInstance: Singleton
};
